package com.github.browep.bat.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.github.browep.bat.R;
import com.github.browep.bat.activities.Locations;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: paul
 * Date: 8/28/11
 * Time: 9:58 AM
 * To change this template use File | Settings | File Templates.
 */
public class UiUtilities {
    public static final String TAG = UiUtilities.class.getSimpleName();

    public static List<View> findAllByTag(ViewGroup parent, String tagValue) {
        List<View> viewList = new LinkedList<View>();
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            if (tagValue.equals(view.getTag())) {
                viewList.add(view);
            }
            if (view instanceof ViewGroup) {
                viewList.addAll(findAllByTag((ViewGroup) view, tagValue));
            }
        }
        return viewList;
    }


    public static AlertDialog locationMultiPopup(final Object phone, final Object lng, final Object lat, final String address, final String hours, final Context context) {
        CharSequence[] options;
        if (hours != null) {
            options = new CharSequence[3];
            options[0] = "Call";
            options[1] = "View on Map";
            options[2] = "See Hours";
        } else {
            options = new CharSequence[2];
            options[0] = "Call";
            options[1] = "View on Map";
        }

        return new AlertDialog.Builder(context)
                .setItems(options, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        switch (which) {
                            case 0:
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                                context.startActivity(intent);
                                break;
                            case 1:
                                String uri = "geo:" + lat + "," + lng;
                                try {
                                    uri += "?q=" +
                                            URLEncoder.encode(address, "UTF-8");
                                } catch (UnsupportedEncodingException e) {
                                    Log.e(Locations.LOG_TAG, "error encoding " + address, e);
                                }
                                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(uri)));
                                break;
                            case 2:
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle("Hours");
                                builder.setInverseBackgroundForced(true);
                                builder.setMessage(hours);
                                builder.create().show();
                                break;
                        }

                    }
                })
                .create();
    }


    public static void setCustomFont(View view, AttributeSet attrs, Context context) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.com_github_browep_bat_widgets_FontableTextView);
        if (a != null) {
            String customFont = a.getString(R.styleable.com_github_browep_bat_widgets_FontableTextView_customFont);

            if (customFont != null) {
                Typeface tf = null;
                try {
                    tf = Typeface.createFromAsset(context.getAssets(), "fonts/" + customFont);
                    if (view instanceof Button)
                        ((Button) view).setTypeface(tf);
                    else
                        ((TextView) view).setTypeface(tf);

                    Log.i(TAG,"loaded: " + tf);
                } catch (Exception e) {
                    Log.e(TAG, "trouble loading " + customFont, e);
                }finally {
                    a.recycle();
                    return;
                }
            }
        }
        Log.e(TAG,"could not load typeface for: " + view);
    }
}
